// shopPages/Refund/Refund.js
import {
  tags_list,
  update_goodsInfo,
  ftp_file,
  baseUrl,
  del_file,
  del_user_img,
  get_goodsInfo,
  del_goods_img,
  ftp_goodsFile
} from "../../utils/api.js"
var app = getApp();
var btn_status = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: [],
    section_scroll:true,
    huowuly:'',
    tuik_yy:'',
    items: [
      {  value: '未收到货' },
      {  value: '已收到货' },
    ],
    items_yyin: [
      { value: '不喜欢/不想要' },
      { value: '空包裹' },
      { value: '未按时约定时间发货' },
      { value: '快递/物流一直未送到' },
      { value: '快递/物流无跟踪记录' },
      { value: '空包裹' },
      { value: '货物破损已拒签' }
    ],
    zhezyinc:false,
    zhezyinc_yy:false,
    img:[],
    Pictuer: {
      // type: 0,
      srcList: []
    },
    list:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var key_name = '';
    if (options.key_name != null && options.key_name != 'null'){
      key_name = options.key_name;
    }else{
      key_name='无';
    }
    that.setData({
      order_id : options.order_id,
      goods_id : options.goods_id,
      title : options.title,
      img_url : options.img_url,
      key_name: key_name,
      refund_type: options.refund_type
    });
    btn_status = true;
  },
  toGoodsDetail: function (e) {
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
    })
  },
  // 点击选择遮罩弹出
  bindztai:function(e){
    this.setData({
      zhezyinc: true,
    })
  },
  bindyuany: function (e) {
    this.setData({
      zhezyinc_yy: true,
    })
  },
  // 点击遮罩隐藏
  radiofalse:function(){
    console.log(1111)
    this.setData({
      zhezyinc: false,
      zhezyinc_yy:false
    })
  },
  // 单选
  radioChange: function (e) {
    this.setData({
      huowuly: e.detail.value,
    })
  },
  radioChange1: function (e) {
    console.log(e.detail.value);
    this.setData({
      tuik_yy: e.detail.value
    })
  },
  descInput:function(e){
    console.log(e.detail.value);
  },
  //添加图片
  selectFile1: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.p_index;
    var list = that.data.list;
    wx.chooseImage({
      count: 6,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var imgList = list[index].imgs;
        for (var i in tempFilePaths) {
          imgList.push(tempFilePaths[i])
        }
        list[index].imgs = imgList;
        that.setData({
          list: list
        })
      }
    })
  },
  //删除图片
  delImg: function (e) {
    const that = this
    const id = e.currentTarget.dataset.id
    const idx = e.currentTarget.dataset.idx
    let changed = new Object()
    let { Pictuer } = that.data
    if (Pictuer.srcList.length > 1) {
      Pictuer.srcList.splice(idx, 1);
      changed['Pictuer.srcList'] = Pictuer.srcList
    } else {
      changed['Pictuer.srcList'] = []
    }
    that.setData(changed)
  },

  // 选择图片
  selectFile: function (e) {
    const that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths[0]
        // if (e.currentTarget.dataset.type == 1){
        //   wx.navigateTo({
        //     url: '/pages/cutPicture/cutPicture?img=' + tempFilePaths,
        //   })
        // } else {
        that.ftpFile({
          // type: e.currentTarget.dataset.type,
          src: tempFilePaths,
          // uid: that.data.userInfo.id,
        })
        // }
      },
      complete(res) {
        // console.log(res)
      }
    })
  },
  // 文件提交
  ftpFile: function (params) {
    const that = this
    wx.uploadFile({
      url: baseUrl + ftp_goodsFile, // 仅为示例，非真实的接口地址
      filePath: params.src,
      name: 'file',
      formData: {
        // uid: params.uid,
        // type: params.type,
        length: params.length
      },
      success(res) {
        let json_res = JSON.parse(res.data)
        let changed = {}
        let { Pictuer } = that.data

        Pictuer.srcList.push({
          img_url: json_res.url,
        });
        changed['Pictuer.srcList'] = Pictuer.srcList
        that.setData(changed)
      },
      fail(err) {
        console.log(err)
      }
    })
  },
  submit:function(){
    var that = this;
    if (btn_status){
      btn_status = false;
      var tuik_yy = that.data.tuik_yy;
      console.log(that.data.tuik_yy);
      var img_list = that.data.Pictuer.srcList;
      if (tuik_yy == '') {
        wx.showToast({
          title: '请选择原因!',
          icon: 'none',
          duration: 1000
        })
        btn_status = true;
        return false;
      }
      var url2 = '/order/updateOrderStatus_v4';
      var params2 = {
        id: that.data.order_id,
        goods_id: that.data.goods_id,
        type: 6,
        invite_id: app.globalData.shareOptions.share_id,
        invite_uid: app.globalData.shareOptions.carte_id,
        refund_type: that.data.refund_type,
        reason_desc: tuik_yy,
        reason_img: img_list,
        timestamp: new Date().getTime(),
      };
      params2.sign = app.getSign(params2)
      app.HttpPostSend(url2, params2, function (res) {
        if (res.code < 0) {
          wx.showToast({
            title: '申请失败!',
            icon: 'none',
            duration: 1000
          })
          btn_status = true;
        } else {
          wx.showModal({
            title: '提示',
            content: '申请成功',
            showCancel: false,
            success: function () {
              wx.navigateBack({
                url: '/pages/myOrders/myOrders?ordertype=退换售后'
              })
            }
          })
          // app.showTipMsg('申请成功');

        }
      });
    }
    
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})